package com.order.aop;


import com.order.exception.CustomException;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class HeaderAspect {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RedisTemplate redisTemplate;
    String sessionid=null;
    @Before("execution( * com.order.rest.OrderRest.*(..))")
    public void Header() throws Throwable{
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest(); //获取请求对象
        String sessionid= (String) redisTemplate.opsForValue().get("SessionId");
        if(!request.getHeader("SessionId").equals(sessionid)){
            logger.info("SessionId未通过:{}",request.getHeader("SessionId"));
            throw new CustomException("SessionId未通过",CustomException.ExceptionName.Forbidden);
        }
    }
}
